
#import "Util.h"
#import <CommonCrypto/CommonDigest.h>

@implementation Util

+ (NSString*)getDateString:(NSInteger) offset {
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"MM/dd"];
    NSDate *datenow = [NSDate date];
    NSInteger interval = offset * 24 * 60 * 60;
    NSDate *lastDay = [NSDate dateWithTimeInterval:interval sinceDate:datenow];
    NSString *currentTimeString = [formatter stringFromDate:lastDay];
    return currentTimeString;
}

+ (NSString*) getTodayString {
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"YYYY/MM/dd"];
    NSDate *datenow = [NSDate date];
    NSString *currentTimeString = [formatter stringFromDate:datenow];
    return currentTimeString;
}

+ (NSString *) getTodayDate {
    NSDate *date = [NSDate date];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyyMMdd"];
    NSString *dateTime = [formatter stringFromDate:date];
    return dateTime;
}

+ (NSString*) getOffsetDate:(NSInteger) offset {
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyyMMdd"];
    NSDate *datenow = [NSDate date];
    NSInteger interval = offset * 24 * 60 * 60;
    NSDate *lastDay = [NSDate dateWithTimeInterval:interval sinceDate:datenow];
    NSString *currentTimeString = [formatter stringFromDate:lastDay];
    return currentTimeString;
}

+(NSString *)md5:(NSString *)string{
//    const char *cStr = [string UTF8String];
//    unsigned char digest[CC_MD5_DIGEST_LENGTH];
//
//    CC_MD5(cStr, (CC_LONG)strlen(cStr), digest);
//
//    NSMutableString *result = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2];
//    for (int i = 0; i < CC_MD5_DIGEST_LENGTH; i++) {
//        [result appendFormat:@"%02X", digest[i]];
//    }
//
//    return result;
//
    const char *cStr = [string UTF8String];
    unsigned char result[16];
    
    NSNumber *num = [NSNumber numberWithUnsignedLong:strlen(cStr)];
    CC_MD5( cStr,[num intValue], result );
    
    return [[NSString stringWithFormat:
             @"%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
             result[0], result[1], result[2], result[3],
             result[4], result[5], result[6], result[7],
             result[8], result[9], result[10], result[11],
             result[12], result[13], result[14], result[15]
             ] lowercaseString];
}
@end
